/**
 * 
 * @Project le cOeur - Data Virtualization
 * @Date 08.12.2012
 * @Author Aebi Pascal, Kocher Marc
 * @School BFH-TI Biel, Bachelor Thesis
 *
 */

package main.implementation.method;

import java.sql.SQLException;
import java.util.List;

import main.framework.connector.MySQLConnector;
import main.framework.exception.ResourceMethodExecutionException;
import main.framework.resource.DataGetterResourceMethod;
import main.framework.resource.Resource;
import main.framework.resource.ResourceData;

/**
 * This class gets the address of the battery.
 */
public class GetBatteryAddress extends DataGetterResourceMethod {

	private MySQLConnector sqlConnector;
	
	@Override
	public void execute(Resource resource) throws ResourceMethodExecutionException{
		int batteryID = 1;
		this.sqlConnector = new MySQLConnector();
		try {
			this.sqlConnector.connect();
			String query = "SELECT address.name, address.street, address.plz, address.city FROM battery, address WHERE battery.id = " + batteryID + " AND battery.addressid = address.id";
			List<String[]> data = sqlConnector.executeGetQuery(query);
			super.data = new ResourceData(data, resource);
			this.sqlConnector.disconnect();
		} catch (SQLException e) {
			try {
				Thread.currentThread().sleep(1000);
				this.sqlConnector.connect();
			} catch (Exception e1) {
				throw new ResourceMethodExecutionException(e.getMessage());
			}
		} catch (ClassNotFoundException e) {
			throw new ResourceMethodExecutionException(e.getMessage());
		}
	}

}
